package ytt.small.fore.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: small_woods
 * @Date: 2020/6/16 21:00
 */
@Slf4j
@Configuration
public class TokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        if (uri.contains("/login")) {
            return true;
        }

        HttpSession session = request.getSession();

        System.out.println( session.getAttribute("userToken"));
        //判断token
        if(session == null || session.getAttribute("userToken") == null){
            log.info("token为空");
//            throw new Exception("错啦！！！！！");
            return false;
        }
        log.info("session: {}",session.getAttribute("userToken"));

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
